package pc

import (
	. "chess_platform/internal/common"
	"fmt"
)

//玩家邮件列表
func ListEmail(p ListParams) ([]*Email, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	o := NewDBOrm(DBChess)
	sql := fmt.Sprintf(`
			SELECT
				*
			FROM
				%v
			ORDER BY
				eid
			DESC
			LIMIT ?,?
		`, TableEmailUser)
	var emails []*Email
	_, err := o.Raw(sql, limit, p.PageSize).QueryRows(&emails)
	if CheckNoExist(err) {
		return nil, 0, nil
	}
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v `, TableEmailUser)
	var count int64
	err = o.Raw(sql).QueryRow(&count)
	return emails, count, err
}

//更新玩家邮件
func UpdateEmail(email map[string]interface{}, eid int64) error {
	o := NewDBOrm(DBChess)
	condition := fmt.Sprintf(`eid = %v`, eid)
	values, sql := UpdateSql(TableEmailUser, email, condition)
	_, err := o.Raw(sql, values).Exec()
	return err
}

func EmailInfoById(eid int64) (*Email, error) {
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			eid = ?
		LIMIT 1
		`, TableEmailUser)
	o := NewDBOrm(DBChess)
	var email Email
	err := o.Raw(sql, eid).QueryRow(&email)
	if CheckNoExist(err) {
		return nil, nil
	}
	return &email, err
}

//删除玩家邮件
func DeleteEmail(eid int64) (int64, error) {
	o := NewDBOrm(DBChess)
	condition := fmt.Sprintf("eid=%v", eid)
	sql := DeleteSql(TableEmailUser, condition)
	result, err := o.Raw(sql).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

//发送全服邮件,其实就是插入一条邮件记录
func NewEmailFull(email map[string]interface{}) (int64, error) {
	o := NewDBOrm(DBChess)
	values, sql := InsertSql(TableEmailFull, email)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.LastInsertId()
}

//更新全服邮件
func UpdateEmailFull(email map[string]interface{}, efid int64) error {
	o := NewDBOrm(DBChess)
	condition := fmt.Sprintf(`efid = %v`, efid)
	values, sql := UpdateSql(TableEmailFull, email, condition)
	_, err := o.Raw(sql, values).Exec()
	return err
}

func EmailFullInfoById(efid int64) (*EmailFull, error) {
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			efid = ?
		LIMIT 1
		`, TableEmailFull)
	o := NewDBOrm(DBChess)
	var email EmailFull
	err := o.Raw(sql, efid).QueryRow(&email)
	if CheckNoExist(err) {
		return nil, nil
	}
	return &email, err
}

//全服邮件列表
func ListEmailFull(p ListParams, t ListDayParams, nickname string, status, publishStatus int64) ([]*EmailFull, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	o := NewDBOrm(DBChess)
	sql := fmt.Sprintf(`
			SELECT
				*
			FROM
				%v
			ORDER BY
				efid
			DESC
			LIMIT ?,?
		`, TableEmailFull)
	var emails []*EmailFull
	_, err := o.Raw(sql, limit, p.PageSize).QueryRows(&emails)
	if err != nil {
		return nil, 0, err
	}
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v `, TableEmailFull)
	var count int64
	err = o.Raw(sql).QueryRow(&count)
	return emails, count, err
}

//删除全服邮件
func DeleteEmailFull(efid int64) (int64, error) {
	o := NewDBOrm(DBChess)
	condition := fmt.Sprintf("efid=%v", efid)
	sql := DeleteSql(TableEmailFull, condition)
	result, err := o.Raw(sql).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}
